package com.memexue.product.service.impl;

import com.memexue.common.DecreaseStockInput;
import com.memexue.common.ProductInfoOutput;
import com.memexue.core.util.JacksonUtils;
import com.memexue.product.dao.ProductInfoRepository;
import com.memexue.product.enums.ProductStatusEnum;
import com.memexue.product.enums.ResultEnum;
import com.memexue.product.exception.ProductException;
import com.memexue.product.po.ProductInfo;
import com.memexue.product.service.ProductService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.core.AmqpTemplate;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.transaction.Transactional;
import java.util.ArrayList;
import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;

@Service
@Slf4j
public class ProductServiceImpl implements ProductService {
    @Autowired
    private ProductInfoRepository productInfoRepository;
    @Autowired
    private AmqpTemplate amqpTemplate;

    @Override
    public List<ProductInfo> findUpAll() {
        return productInfoRepository.findByProductStatus(ProductStatusEnum.UP.getCode());
    }

    @Override
    public List<ProductInfoOutput> findByProductIdIn(List<String> productIdList) {
        return productInfoRepository.findByProductIdIn(productIdList).stream()
                .map(e -> {
                    ProductInfoOutput output = new ProductInfoOutput();
                    BeanUtils.copyProperties(e, output);
                    return output;
                })
                .collect(Collectors.toList());
    }

    @Override
    public void decreaseStock(List<DecreaseStockInput> decreaseStockInputList) {
        List<ProductInfo> productInfoList = decreaseStockProcess(decreaseStockInputList);
        // 发送mq消息
        try {
            List<ProductInfoOutput> outputList = productInfoList.stream().map(e -> {
                ProductInfoOutput output = new ProductInfoOutput();
                BeanUtils.copyProperties(e, output);
                return output;
            }).collect(Collectors.toList());
            amqpTemplate.convertAndSend("productInfo", JacksonUtils.obj2json(outputList));
        } catch (Exception e) {
            log.error("send mq error", e);
        }
    }
    @Transactional
    public List<ProductInfo> decreaseStockProcess(List<DecreaseStockInput> decreaseStockInputList) {
        List<ProductInfo> productInfoList = new ArrayList<>();
        for (DecreaseStockInput cartDto : decreaseStockInputList) {
            // 查商品
            Optional<ProductInfo> productInfoOptional = productInfoRepository.findById(cartDto.getProductId());
            if (!productInfoOptional.isPresent()) {
                // 商品不存在
                throw new ProductException(ResultEnum.PRODUCT_NOT_EXIST);
            }
            ProductInfo productInfo = productInfoOptional.get();
            int result = productInfo.getProductStock() - cartDto.getProductQuantity();
            if (result < 0) {
                // 库存不够
                throw new ProductException(ResultEnum.PRODUCT_STOCK_ERROR);
            }
            productInfo.setProductStock(result);
            // 更新库存信息
            productInfoRepository.save(productInfo);
            productInfoList.add(productInfo);
        }
        return productInfoList;
    }
}